package model

import (
	"encoding/base64"
	"fmt"
	"ginblog/utils/errmsg"
	"github.com/jinzhu/gorm"
	"golang.org/x/crypto/scrypt"
	"log"
)

type User struct {
	BaseModel
	Username string  `gorm:"type:varchar(20); not null" json:"username,omitempty" validate:"required,min=4,max=12" label:"用户名"`
	Password string `gorm:"type:varchar(20); not null" json:"password,omitempty" validate:"required,min=6,max=20" label:"密码"`
	Role int `gorm:"type:int;DEFAULT:2" json:"role,omitempty" validate:"required,gte=2" label:"角色码"`
	Fans int `json:"fans"`
	Follows int `json:"follows"`
}


// 查询用户是否存在
func CheckUser(name string)(code int) {
	var users User
	db.Select("id").Where("username = ?",name).First(&users)
	if users.ID > 0{
		return errmsg.ERROR_USERNAME_USED
	}
	return errmsg.SUCCSE
}


// 新增用户
func CreateUser(data *User)int  {
	//data.Password = ScryptPw(data.Password)
	//data.Before()
	err := db.Create(&data).Error
	if err !=nil {
		return errmsg.ERROR
	}
	return errmsg.SUCCSE
}

// 查询用户列表
func GetUsers(pageSize int, pageNum int)([]User,int) {
	var users []User
	var total int
	err = db.Limit(pageSize).Offset((pageNum-1)*pageSize).Select("id,username,created_at,role").Find(&users).Count(&total).Error
	if err != nil && err != gorm.ErrRecordNotFound{
		return nil,0
	}
	return users,total
}

// 编辑用户 拿到用户的id,修改昵称
func EditUser(id int,data *User) int  {
	var user User
	var maps = make(map[string]interface{})
	maps["username"] = data.Username
	maps["role"] = data.Role
	err = db.Model(&user).Where("id = ?",id).Update(maps).Error
	if err != nil{
		return errmsg.ERROR
	}
	return errmsg.SUCCSE
}

// 删除用户
func DeleteUser(id int) int  {
	var user User
	err = db.Where("id = ? ", id).Delete(&user).Error
	if err != nil {
		return errmsg.ERROR
	}
	return errmsg.SUCCSE
}

// 密码加密 钩子函数
func (u *User)BeforeSave()  {
	u.Password = ScryptPw(u.Password)
}

//func (u *User)Before(){
//	u.Password = ScryptPw(u.Password)
//}
func ScryptPw(password string)string {
	const KeyLen = 10
	salt :=make([]byte,8)
	salt = []byte{12,32,4,6,66,22,222,11}

	HashPw,err := scrypt.Key([]byte(password),salt,16384,8,1,KeyLen)
	if err != nil {
		log.Fatal(err)
	}
	fpw := base64.StdEncoding.EncodeToString(HashPw)
	return fpw
}

// 登录验证
func CheckLogin(username string,password string) (int,int){
	var user User

	db.Where("username = ?",username).First(&user)
	fmt.Printf("user:%#v\n",user)
	if user.ID == 0{
		return errmsg.ERROR_USER_NOT_EXIST,0
	}
	if ScryptPw(password) != user.Password{
		return errmsg.ERROR_PASSWORD_WRONG,0
	}
	if user.Role != 1 {
		return errmsg.ERROR_TOKEN_NO_RIGHT,0
	}
	fmt.Printf("user11:%#v\n",int(user.ID))
	return errmsg.SUCCSE,int(user.ID)

}

// 粉丝数+1
func IncrUserFan(id int) int {
	var user User
	db.Model(&user).Where("id = ? ", id).UpdateColumn("fans", gorm.Expr("fans + ?", 1))

	return errmsg.SUCCSE
}


// 粉丝数-1
func SubUserFan(id int) int {
	var user User
	err = db.Model(&user).Where("id = ? ", id).UpdateColumn("fans", gorm.Expr("fans - ?", 1)).Error
	if err != nil {
		return errmsg.ERROR
	}
	return errmsg.SUCCSE
}

// 关注数+1
func IncrUserFollow(id int) int {
	var user User
	db.Model(&user).Where("id = ? ", id).UpdateColumn("follows", gorm.Expr("follows + ?", 1))

	return errmsg.SUCCSE
}


// 关注数-1
func SubUserFollow(id int) int {
	var user User
	err = db.Model(&user).Where("id = ? ", id).UpdateColumn("follows", gorm.Expr("follows - ?", 1)).Error
	if err != nil {
		return errmsg.ERROR
	}
	return errmsg.SUCCSE
}
